<?
$params = array();
$options = array();
$list_info = array();
$num_pages = 0;

$min_year = getMinYear($GLOBALS['id']);
if(!is_numeric($min_year)){
	$min_year = date('Y');
}

$num_years = date('Y') - $min_year + 1;

$options['group_by'] = array('order_id', 'sale_number', 'sale_type', 'sale_date', 'sale_date_', 'sum_amount_total', 'sum_amount_wholesale', 'payment_method', 'payment_currency', 'client_id', 'client_country', 'client_vat_num');
if($GLOBALS['list_info']){
	$list_info = $GLOBALS['list_info'];
	if($list_info['month'] or $list_info['year']){
		$params['sale_date__'] = "%$list_info[year]-$list_info[month]";
	}
}

if(!is_numeric($GLOBALS['limit']) or !is_numeric($GLOBALS['offset'])){
	$options['limit'] = 20;
	$options['offset'] = 0;
}else{
	$options['limit'] = $GLOBALS['limit'];
	$options['offset'] = $GLOBALS['offset'];
}

if($list_info){
	$general_result = getGeneralResults($params, $options);
}
//$general_result = getGeneralResults($params, $options);
//print_r(getGeneralResults($params, $options, 'query'));
if($general_result['num_rows']){
	$num_pages = ceil($general_result['num_rows'] / $options['limit']);
	$page = $options['offset'] / $options['limit'] + 1;
}

?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td align="left">
	  <p><strong>The Whole List of Invoices & Credit Notes</strong><br>
		All Invoices &amp; Credit Notes are sorted according to the date on which they are issued. You can sort them in an ascending or descending order.<br>
	  Click on each Invoice/Credit Note and it will open in a new window.<br>
	Click on the Order ID if you want to see more info about the original Order ID together with the whole billing history.</p></td>
	<td align="right"></td>
  </tr>
</table>
<form name='listInvoices' id='listInvoices' onSubmit='return false;'>
<table class="reg_table" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td width="110">Month:<br />
		<select name='list_info[month]'>
			<option value="">All Months</option>
			<option value="01" <? if($list_info['month']== '01'){ echo 'selected'; } ?>>January</option>
			<option value="02" <? if($list_info['month']== '02'){ echo 'selected'; } ?>>February</option>
			<option value="03" <? if($list_info['month']== '03'){ echo 'selected'; } ?>>March</option>
			<option value="04" <? if($list_info['month']== '04'){ echo 'selected'; } ?>>April</option>
			<option value="05" <? if($list_info['month']== '05'){ echo 'selected'; } ?>>May</option>
			<option value="06" <? if($list_info['month']== '06'){ echo 'selected'; } ?>>June</option>
			<option value="07" <? if($list_info['month']== '07'){ echo 'selected'; } ?>>July</option>
			<option value="08" <? if($list_info['month']== '08'){ echo 'selected'; } ?>>August</option>
			<option value="09" <? if($list_info['month']== '09'){ echo 'selected'; } ?>>September</option>
			<option value="10" <? if($list_info['month']== '10'){ echo 'selected'; } ?>>October</option>
			<option value="11" <? if($list_info['month']== '11'){ echo 'selected'; } ?>>November</option>
			<option value="12" <? if($list_info['month']== '12'){ echo 'selected'; } ?>>December</option>
		</select>
	</td>
	<td width="70">Year:<br />
		<select name='list_info[year]'>
			<option value="">Any</option>
			<?
			for($i = 0; $i < $num_years; $i++){
				$year = date("Y", mktime(0, 0, 0, date('m'), 1, date('Y') - $i));
				$selected = $year == $list_info['year'] ? 'selected' : '';
				echo "<option value='$year' $selected>$year</option>";
			}
			?>
		</select>
	</td>
	<td>
		<br />
		<input type='hidden' name='num_pages' value='<?=$num_pages?>' id='num_pages'>
		<input type='hidden' name='limit' value='<?=$options['limit']?>' id='limit'>
		<input type='hidden' name='offset' value='<?=$options['offset']?>' id='offset'>
		<input type="submit" value="View" onClick="unsetLimitOffset();openPage('lists.html', getFormData('listInvoices'));" id='submit_button'>
	</td>
  </tr>
</table>
<input type='hidden' id='searchFormId' value='listInvoices'>
</form>
<br />
<?
$for_month = $list_info['month'] ? date('F', mktime(0, 0, 0, $list_info['month'], 1, 1)) : 'All Months';
$for_year = $list_info['year'] ? $list_info['year'] : 'Any Year';

if($general_result['result']){
	echo "<img src='images/arrow_red.jpg' alt='Results' width='9' height='9'> Results for <strong>$for_month</strong> during <strong>$for_year</strong><br><br>";
	echo "<table class='bTable' cellspacing=0 cellpadding=0 width=100%>
		<tr>
			<th>Order ID</th>
			<th>Date</th>
			<th>Document Type</th>
			<th>Amount</th>
		";

	foreach($general_result['result'] as $k => $v){
		$formated_date = date('M d, Y', strtotime($v['sale_date']));
		$retail = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_total'];
		$wholesale = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_wholesale'];

		$inv_arr['order_id'] = $v['order_id'];
		$inv_arr['client_country'] = $v['client_country'];
		$inv_arr['client_vat_num'] = $v['client_vat_num'];
		$inv_arr['payment_method'] = $v['payment_method'];

		if($v['sale_type'] != 'reversal'){
			$inv_arr['invoice_type'] = 'invoice';
			$inv_arr['index'] = '';
			$invoice_number = generate_invoice_number($inv_arr);
                        $params['sid'] = $sid;
                        $params['action'] = 'view_invoice';
                        $params['invoice_number'] = preg_replace('/-x/', '-c', $invoice_number);
                        $url_reseller = $GLOBALS['baseURL'].'/?'.encode_params($params);
                        $params['invoice_number'] = preg_replace('/-a/', '-c', $invoice_number);
                        $url_client = $GLOBALS['baseURL'].'/?'.encode_params($params);
			$inv_wh = "<a href=\"$url_reseller\" target=\"_blank\">Invoice No. ".preg_replace('/-x/', '-c', $invoice_number)."</a>";
			$inv_r = "<a href=\"$url_client\" target=\"_blank\">Invoice No. ".preg_replace('/-x/', '-a', $invoice_number)."</a>";
		}else{
			$inv_arr['invoice_type'] = 'credit_note';
			preg_match('/ref[\d]+$/', $v['sale_number'], $matches);
			$inv_arr['index'] = preg_replace('/ref/', '', $matches[0]);
			$invoice_number = generate_invoice_number($inv_arr);
			$retail = '<font class="red">'.$retail.'</font>';
			$wholesale = '<font class="red">'.$wholesale.'</font>';
			$params['sid'] = $sid;
			$params['action'] = 'view_invoice';
			$params['invoice_number'] = preg_replace('/-x/', '-c', $invoice_number);
			$url_reseller = $GLOBALS['baseURL'].'/?'.encode_params($params);
			$params['invoice_number'] = preg_replace('/-a/', '-c', $invoice_number);
			$url_client = $GLOBALS['baseURL'].'/?'.encode_params($params);
			$inv_wh = "<a href=\"$url_reseller\" target=\"_blank\"><font class=\"red\">Credit Note No. ".preg_replace('/-x/', '-c', $invoice_number)."</font></a>";
			$inv_r = "<a href=\"$url_client\" target=\"_blank\"><font class=\"red\">Credit Note No. ".preg_replace('/-x/', '-a', $invoice_number)."</font></a>";
		}

		//print_r($inv_arr);

		echo "<tr>
				<td valign='top' align='center'><a href=\"#\" onClick=\"openPage('account(billing).html','info[order_id]=$v[order_id]&info[client_id]=$v[client_id]' + '&return_to=openPage(\'lists.html\',\'' + getFormData('listInvoices').replace(/&/g,':amp:') + '\');'); return false;\">$v[order_id]</a></td>
				<td valign='top' align='center'>$formated_date</td>
				<td valign='top' align='center'>
					$inv_r<br>
					$inv_wh
				</td>
				<td valign='top' align='right'>
					$retail<br>
					$wholesale
				</td>
			</tr>";
	}
?>
<br />
<table align="center" width="80%"  border="0" cellspacing="0" cellpadding="0">
	<tr>
	  <td height="21" width="21">
		<?
		if($page == 1){
			echo "<img src='images/result_arrow_disabled_left.jpg' alt='View Previous Results Page' width='21' height='21' border='0' />";
		}else{
			echo "<a href='#' onClick=\"page('prev');openPage('lists.html', getFormData('listInvoices'));return false;\"><img src='images/result_arrow_enabled_left.jpg' alt='View Previous Results Page' width='21' height='21' border='0' /></a>";
		}
		?>
	  </td>
	  <td width="180" class="black" style="padding-left:7px; ">Results page: <span class="red style2"><strong><?=$page?></strong></span> of <span class="black style2"><strong><?=$num_pages?></strong></span></td>
	  <td width="21">
		<?
		if($page == $num_pages){
			echo "<img src='images/result_arrow_disabled_right.jpg' alt='View Next Results Page' width='21' height='21' border='0' />";
		}else{
			echo "<a href='#' onClick=\"page('next');openPage('lists.html', getFormData('listInvoices'));return false;\"><img src='images/result_arrow_enabled_right.jpg' alt='View Next Results Page' width='21' height='21' border='0' />";
		}
		?>
	  </td>
	  <td width="23"></td>
		<td class="black" width="142" align="right" style="padding-right:7px; ">Go Directly to page: </td>
		<td width="40" align="left"><input name="" id='gotopage' class="black" style="width:34px; height: 18px; padding-left:7px; border: 1px solid #89B4CC; font-weight:bold; color:#0000CC;"></td>
		<td width="28" style="padding-left:7px; "><a href="#" onClick="page(document.getElementById('gotopage').value);openPage('lists.html', getFormData('listInvoices'));return false;"><img src="images/go2page_arrow.jpg" alt="View Selected Page" width="21" height="21" border="0"></a></td>
	</tr>
</table>
<?
}elseif($list_info){
	include($GLOBALS['path'].'no_results.html');
}
?>
<br>